package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.config.AjaxResult;
import com.example.demo.pojo.SysRole;
import com.example.demo.service.SysRoleService;
import com.example.demo.mapper.SysRoleMapper;
import com.example.demo.uilt.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 *
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
@Autowired
private SysRoleService sysRoleService;
  @Override
  public AjaxResult roleLists(SysRole sysRole, Integer pageIndex, Integer pageSize) {
    IPage<SysRole> page=new Page<>(pageIndex,pageSize);
    QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("role_name",sysRole.getRoleName());
    queryWrapper.eq("role_key",sysRole.getRoleKey());
    queryWrapper.eq("status",sysRole.getStatus());
    IPage<SysRole> list = sysRoleService.page(page, queryWrapper);
    HashMap hashMap=new HashMap();
    hashMap.put("list",list);

    if(list!=null){
      return AjaxResult.success("成功",hashMap);
    }else {
      return AjaxResult.error("失败",hashMap);
    }
  }

  @Override
  public ResponseResult roleInsert(SysRole sysRole){

    boolean save = sysRoleService.save(sysRole);
    if (save) {
      return new ResponseResult(200, "成功");
    } else {
      return new ResponseResult(500, "失败");
    }
  }

  @Override
  public ResponseResult roleUpdate(SysRole sysRole) {
    Long roleId = sysRole.getRoleId();
    QueryWrapper<SysRole> sysRoleQueryWrapper=new QueryWrapper<>();
    sysRoleQueryWrapper.eq("role_id",roleId);
    boolean update = sysRoleService.update(sysRole, sysRoleQueryWrapper);
    if(update){
      return new ResponseResult(200,"成功",roleId);
    }else {
      return new ResponseResult(500,"失败",roleId);
    }

  }
}




